package com.lp.module_lib.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lp.module_lib.R;
import com.lp.module_lib.utils.StatusBarUtil;

/**
 * 作者：created by LP_TREAM on 2020/11/27 21:06
 * 邮箱：806875201@qq.com
 */
public class AppTitleBar extends LinearLayout {
    private OnLeftClickListener mLeftClickListener;
    private OnRightClickListener mRightClickListener;
    private MyViewHolder mViewHolder;
    private View viewAppTitle;

    public AppTitleBar(Context context) {
        super(context);
        init();
    }

    public AppTitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public AppTitleBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        viewAppTitle = inflater.inflate(R.layout.view_titlebar, null);
        this.addView(viewAppTitle, layoutParams);

        initLayout();
    }

    private void initLayout() {
        mViewHolder = new MyViewHolder(this);
        if (mViewHolder.mIvLeftImg != null) {
            mViewHolder.mIvLeftImg.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mLeftClickListener != null) {
                        mLeftClickListener.onLeftClick(v);
                    }
                }
            });
        }
        if (mViewHolder.mTvRightName != null) {
            mViewHolder.mTvRightName.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mRightClickListener != null) {
                        mRightClickListener.OnRightClick(v);
                    }
                }
            });
        }
        if (mViewHolder.mViewStatusBar != null) {
            StatusBarUtil.setStatusViewAttr(mViewHolder.mViewStatusBar, getContext());
        }
    }

    /**
     * 设置自定义顶部布局
     *
     * @param layout 布局id
     */
    public void setLayout(int layout) {
        mViewHolder.mRlTitleLayout.removeAllViews();
        View view = View.inflate(getContext(), layout, null);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.WRAP_CONTENT);
        mViewHolder.mRlTitleLayout.addView(view, params);

        initLayout();
    }

    /**
     * 设置自定义顶部布局
     *
     * @param view   布局view
     * @param height 布局高度
     */
    public void setLayout(View view, int height) {
        mViewHolder.mRlTitleLayout.removeAllViews();
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, height);
        mViewHolder.mRlTitleLayout.addView(view, params);

        initLayout();
    }

    /**
     * 获取整个布局控件
     *
     * @return
     */
    public MyViewHolder getViewHolder() {
        return mViewHolder;
    }

    /**
     * 设置背景图片
     *
     * @param res
     */
    public void setBaseImage(int res) {
        mViewHolder.mLlBaseLayout.setBackgroundResource(res);
    }

    /**
     * 设置标题
     *
     * @param title
     */
    public void setTitleName(String title) {
        if (!TextUtils.isEmpty(title)) {
            mViewHolder.mTvTitle.setText(title);
        }
    }

    /**
     * 设置标题栏文字颜色
     *
     * @return
     */
    public void setTitleColor(int color) {
        mViewHolder.mTvTitle.setTextColor(color);
    }

    /**
     * 设置右侧名字
     *
     * @return
     */
    public void setRightTitle(String text) {
        if (!TextUtils.isEmpty(text)) {
            mViewHolder.mTvRightName.setText(text);
        }
    }

    /**
     * 设置左侧返回图标
     *
     * @param sourceID
     */
    public void setLeftIcon(int sourceID) {
        mViewHolder.mIvLeftImg.setImageResource(sourceID);
    }

    /**
     * 设置背景颜色
     *
     * @param color
     */
    public void setAppBackground(int color) {
        viewAppTitle.setBackgroundColor(color);
    }

    public void setOnLeftClickListener(OnLeftClickListener listen) {
        mLeftClickListener = listen;
    }

    public void setOnRightClickListener(OnRightClickListener listen) {
        mRightClickListener = listen;
    }

    public interface OnLeftClickListener {
        void onLeftClick(View v);
    }

    public interface OnRightClickListener {
        void OnRightClick(View v);
    }

    public static class MyViewHolder {
        public LinearLayout mLlBaseLayout;
        public View mViewStatusBar;
        public RelativeLayout mRlTitleLayout;
        public TextView mTvTitle;
        public ImageView mIvLeftImg;
        public TextView mTvRightName;

        public MyViewHolder(View v) {
            mLlBaseLayout = (LinearLayout) v.findViewById(R.id.ll_base_layout);
            mViewStatusBar = (View) v.findViewById(R.id.view_status_bar);
            mRlTitleLayout = (RelativeLayout) v.findViewById(R.id.rl_title_layout);
            mTvTitle = (TextView) v.findViewById(R.id.tvTitle);

            mIvLeftImg = (ImageView) v.findViewById(R.id.ivLeftImg);
            mTvRightName = (TextView) v.findViewById(R.id.tvRightName);

        }
    }
}
